package workSheets;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class workSheet6 {
    public static void main (String[] agrs) {
        List<shape> all=new ArrayList<> ();
        while (true){
            System.out.println ("Enter the graphics you want to create");
            Scanner scanner = new Scanner (System.in);
            all.add (shape.check (scanner.nextLine ()));
            System.out.println ("Do you want to continue adding shapes(Y/N)");
            if (scanner.nextLine ().equalsIgnoreCase ("y")){

            }else {
                System.out.println ("input over!!!");
                break;
            }
        }
        System.out.println (all.size ());
    }
}

abstract class shape {
    public abstract double area ();

    public abstract double perimeters ();

    public static shape check (String type) {
        Scanner scanner = new Scanner (System.in);
        if (type.equalsIgnoreCase ("Circle")) {
            System.out.println ("Enter the radius of the circle");
            String radius = scanner.nextLine ();
            while (!(radius.matches ("\\d+"))){
                System.out.println ("Enter the radius of the circle");
                radius = scanner.nextLine ();
            }
            return new Circle (Double.parseDouble (radius));

        } else if (type.equalsIgnoreCase ("Square")) {
            System.out.println ("Enter the side length of the square");
            String length = scanner.nextLine ();
            while (!(length.matches ("\\d+"))){
                System.out.println ("Enter the side length of the square");
                length = scanner.nextLine ();
            }
            return new Square(Double.parseDouble (length));

        } else if (type.equalsIgnoreCase ("Rectangle")) {
            System.out.println ("Enter the length and width of the rectangle");
            String rectangle= scanner.nextLine ();
            while (!(rectangle.matches ("\\d+,\\d+"))){
                System.out.println ("Enter the length and width of the rectangle");
                rectangle= scanner.nextLine ();
            }
            String[] split = rectangle.split (",");
            return new Rectangle (Double.parseDouble (split[0]),Double.parseDouble (split[1]));

        } else if (type.equalsIgnoreCase ("Triangle")) {
            System.out.println ("Enter three sides of a triangle");
            String sides= scanner.nextLine ();
            while (!(sides.matches ("\\d+,\\d+,\\d+"))){
                System.out.println ("Enter three sides of a triangle");
                sides= scanner.nextLine ();
            }
            String[] split = sides.split (",");
            return new Triangle (Double.parseDouble (split[0]),Double.parseDouble (split[1]),Double.parseDouble (split[2]));

        }else return null;
    }

}

class Circle extends shape {
    private double radius;

    /**
     * @param radius Radius of circle
     */
    public Circle (double radius) {
        this.radius = radius;
    }

    public double getRadius () {
        return radius;
    }

    public void setRadius (double radius) {
        this.radius = radius;
    }

    @Override
    public double area () {
        return Math.round (radius * Math.PI * radius * 100) * 0.01;
    }

    @Override
    public double perimeters () {
        return Math.round (radius * Math.PI * 2 * 100) * 0.01;
    }

}

class Square extends shape {
    private double length;

    /**
     * @param length The side length of a square
     */
    public Square (double length) {
        this.length = length;
    }

    public double getLength () {
        return length;
    }

    public void setLength (double length) {
        this.length = length;
    }

    @Override
    public double area () {
        return Math.round (length * length * 100) * 0.01;
    }

    @Override
    public double perimeters () {
        return Math.round (length * 4 * 100) * 0.01;
    }

}

class Rectangle extends shape {

    private double height;
    private double width;

    /**
     * @param height Length of rectangle
     * @param width  Width of rectangle
     */

    public Rectangle (double height, double width) {
        this.height = height;
        this.width = width;
    }

    public double getHeight () {
        return height;
    }

    public void setHeight (double height) {
        this.height = height;
    }

    public double getWidth () {
        return width;
    }

    public void setWidth (double width) {
        this.width = width;
    }


    @Override
    public double area () {
        return Math.round (width * height * 100) * 0.01;
    }

    @Override
    public double perimeters () {
        return Math.round ((height + width) * 2 * 100) * 0.01;
    }

}


class Triangle extends shape {
    private double a;
    private double b;
    private double c;
    private double p;


    /**
     * @param a One side of a triangle
     * @param b One side of a triangle
     * @param c One side of a triangle
     */

    public Triangle (double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    public double getA () {
        return a;
    }

    public void setA (double a) {
        this.a = a;
    }

    public double getB () {
        return b;
    }

    public void setB (double b) {
        this.b = b;
    }

    public double getC () {
        return c;
    }

    public void setC (double c) {
        this.c = c;
    }

    public boolean check () {
        double a = this.a;
        double b = this.b;
        double c = this.c;
        if (a + b > c && a + c > b && b + c > a) {
            return true;
        }
        return false;
    }

    @Override
    public double area () {
        if (! this.check ()) {
            System.out.println ("input error");
            return 0;
        } else {
            p = (a + b + c) / 2;
            return Math.round (Math.sqrt (p * (p - a) * (p - b) * (p - c)) * 100) * 0.01;
        }
    }

    @Override
    public double perimeters () {
        return Math.round ((a + b + c) * 100) / 0.01;
    }

}

